# -*- coding: utf-8 -*-
# created by Alberthch - 陈航  2021/7/15 19:17
# Description:怎样在数据字典里面执行一些计算操作，比如求最小值，最大值，排序

# 1.字典的value是数值
prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}

# Find min and max price
# 返回一个元组，元组第一个元素应该为数值才可以
min_price = min(zip(prices.values(), prices.keys()))
max_price = max(zip(prices.values(), prices.keys()))
print(type(min_price))  # <class 'tuple'>

print('min price:', min_price)
print('max price:', max_price)

# 返回排序列表，正序排列
prices_sorted = sorted(zip(prices.values(), prices.keys()))
print(type(prices_sorted))  # <class 'list'>
print('sorted prices:')
for price, name in prices_sorted:
    print('    ', name, price)
print("=" * 20)

# 2.键值是数组
students = {
    18: "Tom",
    22: "Mary",
    15: "Jone",
    12: "Bob"
}
# 对字典直接进行数值运算，它只对键值进行操作，并且返回键值
print("年级最大的人：", max(students), students[max(students)])
print("年纪最小的人：", min(students), students[min(students)])
print("年纪排序：")
for age in sorted(students):
    print(age, students[age])
print("=" * 20)
